/**
* @author Ucar Sahin
*
* 03.06.2011
* ProtocolServiceImpl.java
* 
*/

package at.fhv.easyDietWeb.server;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import at.fhv.easyDietWeb.client.protocolService.IProtocolService;
import at.fhv.easyDietWeb.server.controller.useCase.ProtocolController;
import at.fhv.easyDietWeb.shared.exception.useCase.CannotSaveNutritionalJournalException;
import at.fhv.easyDietWeb.shared.exception.useCase.IncorrectInputException;
import at.fhv.easyDietWeb.shared.exception.useCase.InvalidNutritionalJournalException;
import at.fhv.easyDietWeb.shared.exception.useCase.NoFoodFoundException;
import at.fhv.easyDietWeb.shared.exception.useCase.NoMealTypeFoundException;
import at.fhv.easyDietWeb.shared.exception.useCase.NoPatientFoundException;
import at.fhv.easyDietWeb.shared.util.ISessionAttributes;
import at.fhv.easyDietWeb.shared.util.ProtocolData;
import at.fhv.easyDietWeb.shared.util.wrappers.FoodListWrapper;
import at.fhv.easyDietWeb.shared.util.wrappers.FoodWrapper;
import at.fhv.easyDietWeb.shared.util.wrappers.MealTypesWrapper;
import at.fhv.easyDietWeb.shared.util.wrappers.UnitsWrapper;
import at.fhv.easyDietWeb.shared.util.wrappers.UserWrapper;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Servlet, which realizes the methods for the nutritional plan creating
 * @author Spiegel Philipp
 *
 */
public class ProtocolServiceImpl extends RemoteServiceServlet
		implements IProtocolService
{

	@Override
	public MealTypesWrapper getMealTypes()
	{
		ProtocolController pC = new ProtocolController();
		List<String> mealTypes = pC.getMealTypes();
		
		return new MealTypesWrapper(mealTypes);		
	}

	@Override
	public Boolean validateDates(Date from, Date until)
	{
		ProtocolController pC = new ProtocolController();
		HttpServletRequest request = this.getThreadLocalRequest();
		HttpSession session = request.getSession();
		UserWrapper uW = (UserWrapper) session.getAttribute(ISessionAttributes.USER_ATTR);
		
		boolean isValid = pC.validateDates(from, until, uW.getID());
		return isValid;
	}

	@Override
	public int getTimeSpan(Date from, Date until)
	{
		Calendar calFrom = Calendar.getInstance();
		Calendar calUntil = Calendar.getInstance();
		
		calFrom.setTime(from);
		calUntil.setTime(until);
		
		Calendar calTemp = calFrom;
		int timeSpan = 1;
		
		while (calTemp.before(calUntil))
		{
			timeSpan++;
			calTemp.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		return timeSpan;
	}

	@Override
	public FoodListWrapper getFoodList(String name)
	{
		ProtocolController pC = new ProtocolController();
		List<FoodWrapper> foodList = null;
		try
		{
			foodList = pC.getFoodList(name);
			return new FoodListWrapper(foodList);
		} 
		catch (NoFoodFoundException e)
		{
			e.printStackTrace();
			return null;
		}
		catch (IncorrectInputException e)
		{
			e.printStackTrace();
			return null;
		}		
	}

	@Override
	public UnitsWrapper loadUnits()
	{
		ProtocolController pC = new ProtocolController();
		List<String> units = pC.loadUnits();
		
		return new UnitsWrapper(units);
	}

	@Override
	public Boolean saveProtocol(ProtocolData protocol)
	{
		HttpServletRequest request = this.getThreadLocalRequest();
		HttpSession session = request.getSession();
		UserWrapper uW = (UserWrapper) session.getAttribute(ISessionAttributes.USER_ATTR);
		
		ProtocolController pC = new ProtocolController();
		
		try
		{
			pC.saveProtocol(protocol, uW.getID());
			return true;
		} 
		catch (NoPatientFoundException e)
		{
			e.printStackTrace();
			return false;
		} 
		catch (NoMealTypeFoundException e)
		{
			e.printStackTrace();
			return false;
		}
		catch (NoFoodFoundException e)
		{
			e.printStackTrace();
			return false;
		} 
		catch (InvalidNutritionalJournalException e)
		{
			e.printStackTrace();
			return false;
		} 
		catch (CannotSaveNutritionalJournalException e)
		{
			e.printStackTrace();
			return false;
		}		
	}
}